package com.neomanager.admin.web;

import java.util.ArrayList;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.neomanager.admin.dao.BbsDao;
import com.neomanager.login.model.AdminInfo;
import com.util.UtilCommon;

@Controller
@SessionAttributes("adminInfo")
public class BbsController
{
	private final Logger logger = Logger.getLogger(this.getClass());
	@Resource(name="BbsDao")
	BbsDao bbs;
	/**
	 * 게시판 목록 화면
	 */
	@RequestMapping(value = "/admin/bbsmng_list.do", method = RequestMethod.GET)
	public String listBbsmng(@RequestParam Map<String, Object> commandMap, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("게시판 목록.");
	    HttpSession session = request.getSession();
	    AdminInfo aInfo = (AdminInfo) session.getAttribute( "adminInfo" );
		String id = aInfo.getAdmin_id();
		String menuFile = "../menu/" + id + "_menu.jsp";

		// 현재 페이지
		int page = 1;
		if (commandMap.get("page") != null)
			page = Integer.parseInt(commandMap.get("page").toString());

		// 페이지당 목록 수
		int pageCount = 10;

		// 시작번호
		int startNo = (page - 1) * pageCount;

		commandMap.put("page", page);
		commandMap.put("pageCount", pageCount);
		ArrayList<Map<String, Object>> listMap = bbs.selectBbsInfo(commandMap);

		// 총 갯수
		int totalCount = 0;
		if ( listMap != null && listMap.size() > 0 )
		{
			Map<String, Object> rs = listMap.get(0);
			totalCount = Integer.parseInt( rs.get("TOTALCOUNT").toString() );
		}

		// 페이징 처리
		if (totalCount > 0)
		{
			UtilCommon util = new UtilCommon();
			// 총게시물수, 페이지당 목록수, 현재페이지, 블럭당 나타낼 페이지 수
			util.setPage(totalCount, pageCount, page, 10);
			commandMap.put("totalCount", totalCount);
			commandMap.put("totalPage", util.getTotalPage());
			commandMap.put("startPage", util.getStartPage());
			commandMap.put("endPage", util.getEndPage());
			commandMap.put("startNo", startNo);
		}

		model.addAttribute("commandMap", commandMap);
		model.addAttribute("menuinclude", menuFile);
		model.addAttribute("listMap", listMap);

		return "admin/bbsmng_list";
	}

	/**
	 * 게시판 등록 화면
	 */
	@RequestMapping(value = "/admin/bbsmng_write.do", method = RequestMethod.GET)
	public String writeBbsmng(@RequestParam Map<String, Object> commandMap, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("게시판 등록");
		HttpSession session = request.getSession();
		AdminInfo aInfo = (AdminInfo) session.getAttribute( "adminInfo" );
		String id = aInfo.getAdmin_id();
		String menuFile = "../menu/" + id + "_menu.jsp";

		model.addAttribute("menuinclude", menuFile);

		return "admin/bbsmng_write";
	}

	/**
	 * 관리자 수정 화면
	 */
	@RequestMapping(value = "/admin/bbsmng_edit.do", method = RequestMethod.GET)
	public String editBbsmng(@RequestParam Map<String, Object> commandMap, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("게시판 수정 화면.");
	    HttpSession session = request.getSession();
	    AdminInfo aInfo = (AdminInfo) session.getAttribute( "adminInfo" );
		String id = aInfo.getAdmin_id();
		String menuFile = "../menu/" + id + "_menu.jsp";

		Map<String, Object> listMap = bbs.selectBbsInfoById(commandMap);

		model.addAttribute("menuinclude", menuFile);
		model.addAttribute("listMap", listMap);

		return "admin/bbsmng_edit";
	}

	/**
	 * 게시판 등록/수정/삭제 처리
	 */
	@RequestMapping(value = "/admin/bbsmng_proc.do", method = RequestMethod.POST)
	public String processBbsmng(@RequestParam Map<String, Object> commandMap, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("게시판 등록/수정/삭제 처리");

		if(commandMap.get("mode").toString().equals("write"))
		{
			String bbs_id = UtilCommon.idMake("");
			commandMap.put("bbs_id", bbs_id);
			bbs.insertBbsInfo(commandMap);
		}
		else if(commandMap.get("mode").toString().equals("edit"))
		{
			bbs.updateBbsInfo(commandMap);
		}
		else if(commandMap.get("mode").toString().equals("delete"))
		{
			bbs.deleteBbsInfo(commandMap);
		}

		return "redirect:../admin/bbsmng_list.do";
	}
}
